package com.platform.comm.util;

import cn.hutool.core.date.DateUtil;
import com.platform.comm.constants.CMethod;
import com.platform.comm.constants.CParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Objects;

/**
 * Title: ExtSyncUtil
 * Description:库对库字段项转换方法
 * Date 2020/10/28
 *
 * @author lch
 * @version v1.0
 */
@Component
public class ExtSyncUtil {

    /**
     * 字符方法
     */

    @CMethod(title = "获取字段值", type = "string", sort = 1)
    public static String getValue(@CParam(name = "field", title = "源字段", isItem = true) String field) {
        return field;
    }

    @CMethod(title = "字段值修整", type = "string", sort = 2)
    public static String getTrim(@CParam(name = "field", title = "源字段", isItem = true) String field) {
        return field.trim();
    }

    @CMethod(title = "字段值转换大写", type = "string", sort = 3)
    public static String toUpperCase(@CParam(name = "field", title = "源字段", isItem = true) String field) {
        return field.toUpperCase();
    }

    @CMethod(title = "字段值转换小写", type = "string", sort = 4)
    public static String toLowerCase(@CParam(name = "field", title = "源字段", isItem = true) String field) {
        return field.toLowerCase();
    }

    @CMethod(title = "字段值部分替换", type = "string", sort = 5)
    public static String replaceAll(@CParam(name = "str", title = "源字段", isItem = true) String field,
                                    @CParam(name = "str", title = "目标字符") String str1,
                                    @CParam(name = "str", title = "替换字符") String str2) {
        return field.replaceAll(str1, str2);
    }

    @CMethod(title = "字段值截取长度", type = "string", sort = 6)
    public static String substring(@CParam(name = "field", title = "源字段", isItem = true) String field,
                                   @CParam(name = "begin", title = "开始位置", dataType = "int") int begin,
                                   @CParam(name = "end", title = "结束位置", dataType = "int") int end) {
        return field.substring(begin, end);
    }

    @CMethod(title = "字段值替换字符", type = "string", sort = 7)
    public static String replace(@CParam(name = "field", title = "源字段", isItem = true) String field,
                                 @CParam(name = "char1", title = "存在字符", dataType = "char") char char1,
                                 @CParam(name = "char2", title = "替换字符", dataType = "char") char char2) {
        return field.replace(char1, char2);
    }


    /**
     * 数值方法
     */

    @CMethod(title = "绝对值", type = "number", sort = 2)
    public static int getAbs(@CParam(name = "num1", title = "字段一", dataType = "int", isItem = true) int num1) {
        return Math.abs(num1);
    }

    @CMethod(title = "取余数", type = "number", sort = 3)
    public static double getMod(@CParam(name = "x", title = "字段一", dataType = "double", isItem = true) Double x,
                                @CParam(name = "y", title = "字段二", dataType = "double", isItem = true) Double y) {
        return (BigDecimal.valueOf(x).divideAndRemainder(BigDecimal.valueOf(y))[1]).doubleValue();
    }

    @CMethod(title = "取商", type = "number", sort = 4)
    public static int getQuotient(@CParam(name = "x", title = "字段一", dataType = "double", isItem = true) Double x,
                                  @CParam(name = "y", title = "字段二", dataType = "double", isItem = true) Double y) {
        return (BigDecimal.valueOf(x).divideAndRemainder(BigDecimal.valueOf(y))[0]).intValue();
    }

    @CMethod(title = "四舍五入", type = "number", sort = 5)
    public static double getRound(@CParam(name = "x", title = "字段一", dataType = "double", isItem = true) Double x,
                                  @CParam(name = "y", title = "保留位数", dataType = "int", isItem = false) int y) {
        return BigDecimal.valueOf(x).setScale(y, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    @CMethod(title = "获取UUID", type = "string", sort = 9)
    public static String getUUID() {
        return StringUtil.getUUID();
    }

    /**
     * 时间方法
     */

    @CMethod(title = "获取当前日期", type = "time", sort = 1)
    public static Date getCurrentDate() {
        return DateTimeUtil.parseStdShortTime(DateTimeUtil.toStdDateStr(LocalDate.now()));
    }

    @CMethod(title = "获取当前时间", type = "time", sort = 2)
    public static Date getCurrentTime() {
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.now();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        Date date = Date.from(zdt.toInstant());
        return date;
    }

    @CMethod(title = "获取日期中的年份", type = "time", sort = 3)
    public static int getYear(@CParam(name = "date", title = "时间", dataType = "string", isItem = true) Object date) {
        if (date instanceof LocalDate) {
            return ((LocalDate) date).getYear();
        }
        if(date instanceof LocalDateTime) {
            return ((LocalDateTime)date).getYear();
        }
        if(date instanceof LocalDateTime) {
            return ((LocalDateTime) date).getYear();
        }
        if (date instanceof Timestamp) {
            return ((Timestamp) date).toLocalDateTime().getYear();
        }
        if(date instanceof Date) {
            return ((Date) date).getYear() + 1900;
        }
        if(date instanceof java.sql.Date) {
            return ((java.sql.Date)date).getYear() + 1900;
        }
        try {
            return new Date(date.toString()).getYear() + 1900;
        } catch (Exception e) {
            return DateUtil.parse(date.toString()).getYear() + 1900;
        }
    }

    @CMethod(title = "获取日期中的月份", type = "time", sort = 4)
    public static int getMonth(@CParam(name = "date", title = "时间", dataType = "string", isItem = true) Object date) {
        if (date instanceof LocalDate) {
            return ((LocalDate) date).getMonthValue();
        }
        if(date instanceof LocalDateTime) {
            return ((LocalDateTime)date).getMonth().getValue();
        }
        if (date instanceof Timestamp) {
            return ((Timestamp) date).toLocalDateTime().getMonthValue();
        }
        if(date instanceof Date) {
            return ((Date) date).getMonth() + 1;
        }
        try {
            return ((java.sql.Date)date).getMonth() + 1;
        } catch (Exception e) {
            return DateUtil.parse(date.toString()).getMonth() + 1;
        }
    }


    /**
     * 条件
     */

    @CMethod(title = "如果v1为NULL，则返回v2，否则返回v1", type = "condition", sort = 3)
    public static String ifNull(@CParam(name = "v1", title = "参数值一", dataType = "String", isItem = true) String v1,
                                @CParam(name = "v2", title = "参数值二", dataType = "String", isItem = true) String v2) {
        return Objects.isNull(v1) ? v2 : v1;
    }

    @CMethod(title = "如果v1为(空/NULL)，则返回v2，否则返回v1", type = "condition", sort = 2)
    public static String ifEmpty(@CParam(name = "v1", title = "参数值一", dataType = "String", isItem = true) String v1,
                                 @CParam(name = "v2", title = "参数值二", dataType = "String", isItem = true) String v2) {
        return StringUtils.isEmpty(v1) ? v2 : v1;
    }

    @CMethod(title = "如果v1为(空白/空/NULL)，则返回v2，否则返回v1", type = "condition", sort = 1)
    public static String ifBlank(@CParam(name = "v1", title = "参数值一", dataType = "String", isItem = true) String v1,
                                 @CParam(name = "v2", title = "参数值二", dataType = "String", isItem = true) String v2) {
        return StringUtils.isBlank(v1) ? v2 : v1;
    }


}
